/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrameController.java
 *
 * Created on 14/07/2011, 12:04:20
 */
package login;

import chatWindow.ChatWindowFrameController;
import chatWindow.ThreadClient;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author linux
 */
public class LoginFrame extends javax.swing.JInternalFrame {

    /** Creates new form MainFrameController */
    public LoginFrame() {
        initComponents();
        thread_recv();
        login_error = null;
        //setFrameIcon(ICON_LOGIN);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Key_16.png")));
         
    }
    
     private void thread_recv(){
        Runnable newMsg = new ThreadClient();
        Thread thread = new Thread(newMsg);
        thread.start();                
     }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tfUserName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Login");

        jLabel1.setText("User Name:");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Ok_16.png"))); // NOI18N
        jButton1.setText("Accept");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel1)
                .addGap(4, 4, 4)
                .addComponent(tfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(301, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        login_error = null;
        if(tfUserName.getText().compareTo("") == 0){
            JOptionPane.showInternalMessageDialog(this, "you must write a non empty user name.");
        }else{/*User Logged successfull*/
            
           LoginFrameController.getInstance().login(tfUserName.getText()); 
           
           while (login_error == null){
                try {
                    System.out.println("SLEEPING");
                    Thread.sleep(500);
                    Thread.yield();
                } catch (Exception ex) {
                    Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
           }
           if(login_error.intValue() == 0){
               ChatWindowFrameController.getInstance().show(this.getDesktopPane());
               ChatWindowFrameController.getInstance().getCwFrame().setLoginName(tfUserName.getText());
           }else{
               if(login_error.intValue() == 1){
                   JOptionPane.showMessageDialog(LoginFrameController.getInstance().getlFrame(), "User already logged");
               }else{
                   JOptionPane.showMessageDialog(LoginFrameController.getInstance().getlFrame(), "The chatroom is full");
               }
           }
           

           this.dispose();
        }
            
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField tfUserName;
    // End of variables declaration//GEN-END:variables

    public static Integer login_error;
    
    public String getUserName() {
        return tfUserName.getText();
    }

    public void setUserName(String userName) {
        this.tfUserName.setText(userName);
    }
    
    
    
    
//    public static final ImageIcon ICON_ACCEPT =
//    new ImageIcon(ClientConfiguration.getIcons()+"Ok_16.png");
//    
//    public static final ImageIcon ICON_LOGIN =
//    new ImageIcon(ClientConfiguration.getIcons()+"Key_16.png");

}
